package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private SearchService searchService;

    //创建操作es的客户端（模板引擎）
    @GetMapping("inner/createIndex")
    public Result createIndex() {
        //7.8.X启动项目自动创建
        //6.8.X：方法过时
        restTemplate.createIndex(Goods.class);
        restTemplate.putMapping(Goods.class);

        return Result.ok();
    }

    //上架
    @GetMapping("inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }
    //热度排名
    @GetMapping("inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable Long skuId) {
        searchService.updateHostScore(skuId);
        return Result.ok();
    }

    //商品检索
    @PostMapping
    public Result search(@RequestBody SearchParam searchParam){
        //  调用服务层方法
        SearchResponseVo searchResponseVo = null;
        try {
            searchResponseVo = this.searchService.search(searchParam);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //  返回数据
        return Result.ok(searchResponseVo);
    }
}
